package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dpmain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.Activity;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @文件名: UserController
 * @作者: DX
 * @创建日期: 2025/10/7
 * @描述:
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除用户：{}", ids);
        userService.deleteByIds(ids);
        return Result.success();
    }


    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel){
        log.info("根据角色查询用户：{}", roleLabel);
        List<Activity> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("更新用户：{}", user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询用户：{}", id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }
}
